{include file="pub/header"}
<title>添加目录</title>
</head>
<body>
<article class="page-container">
	<form action="" method="post" class="form form-horizontal" id="form-admin-edit">
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>用户名：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="text" class="input-text" value="{$admin.uname}" placeholder="" id="adminName" name="uname">
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>登录密码：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="password" class="input-text" value="{$admin.password}" placeholder="" name="password">
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>用户名称：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="text" class="input-text" value="{$admin.name}" placeholder="" id="name" name="name">
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>联系电话：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="text" class="input-text" value="{$admin.telephone}" placeholder="" id="telephone" name="telephone">
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3">邀请码：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="text" class="input-text" value="{$admin.vip_code}" placeholder="" id="vip_code" name="vip_code">
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>所属部门：</label>
			<div class="formControls col-xs-8 col-sm-9">
                <span class="select-box" style="width: 200px;">
                    <input type="hidden" id="department_pid" name="department_pid">
                    <select id="department_pid1" class="select" size="1"></select>
                </span>
				<span class="select-box" style="width: 200px;">
                    <select id="department_id" name="department_id" class="select" size="1"></select>
                </span>
			</div>
		</div>

		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>角色：</label>
			<div class="formControls col-xs-8 col-sm-9">
				{foreach $roleList as $key=>$role}
				<input type="checkbox" class="input-small" id="role-{$key}" value="{$key}"  onclick="showAgent('{$key}', this)" placeholder="" name="role_id[]" {if in_array($key, $roleArr)}checked{/if}>
				<label for="role-{$key}" class="">{$role}</label>
				{/foreach}
			</div>
		</div>

		<div class="row cl agent" {if !in_array(15, $roleArr)} style="display: none;"{/if}>
		<label class="form-label col-xs-4 col-sm-3">代理商等级：</label>
		<div class="formControls col-xs-8 col-sm-9">
                <span class="select-box" style="width: auto;">
                    <select name="agent_level" class="select">
                        <option value="1" {if $admin.agent_level eq 1}selected{/if}>市级代理</option>
						<option value="2" {if $admin.agent_level eq 2}selected{/if}>区县级代理</option>
                    </select>
                </span>
		</div>
		</div>
		<div class="row cl agent" {if !in_array(15, $roleArr)} style="display: none;"{/if}>
		<label class="form-label col-xs-4 col-sm-3">代理商类型：</label>
		<div class="formControls col-xs-8 col-sm-9">
                <span class="select-box" style="width: auto;">
                    <select name="agent_cate" class="select">
                        <option value="1" {if $admin.agent_cate eq 1}selected{/if}>代理商</option>
						<option value="2" {if $admin.agent_cate eq 2}selected{/if}>兼职代理商</option>
                    </select>
                </span>
		</div>
		</div>
		<div class="row cl agent" {if !in_array(15, $roleArr)} style="display: none;"{/if}>
			<label class="form-label col-xs-4 col-sm-3">推广用户佣金金额：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="text" name="person_money" class="input input-text" value="{$admin.person_money}">
			</div>
		</div>
		<div class="row cl agent" {if !in_array(15, $roleArr)} style="display: none;"{/if}>
			<label class="form-label col-xs-4 col-sm-3">动销首单佣金金额：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="text" name="first_order" class="input input-text" value="{$admin.first_order}">
			</div>
		</div>

		<div class="row cl agent" {if !in_array(15, $roleArr)} style="display: none;"{/if}>
			<label class="form-label col-xs-4 col-sm-3">代理商区域：</label>
			<div class="formControls col-xs-8 col-sm-9">
                <span class="select-box" style="width: auto;">
                    <select name="province_id" class="select province" onchange="getNextArea(this, 'city')">
                        <option value="">请选择</option>
                        {foreach $area["province"] as $item}
                            <option value="{$item.id}" {if $admin.province_id eq $item.id}selected{/if}>{$item.name}</option>
                        {/foreach}
                    </select>
                </span>
				<span class="select-box" style="width: auto;">
                    <select name="city_id"  class="select city" onchange="getNextArea(this, 'country')">
                        <option value="">请选择</option>
						{foreach $area["city"] as $item}
                            <option value="{$item.id}" {if $admin.city_id eq $item.id}selected{/if}>{$item.name}</option>
                        {/foreach}
                    </select>
                </span>
				<span class="select-box" style="width: auto;">
                    <select name="country_id" class="select country">
                        <option value="">请选择</option>
						{foreach $area["country"] as $item}
                            <option value="{$item.id}" {if $admin.country_id eq $item.id}selected{/if}>{$item.name}</option>
                        {/foreach}
                    </select>
                </span>
			</div>
		</div>

		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3">描述：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<textarea name="describe" cols="" rows="" class="textarea"  placeholder="说点什么...100个字符以内" dragonfly="true">{$admin.describe}</textarea>
			</div>
		</div>
		<div class="row cl">
			<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
				<input type="hidden" id="adminId" value="{$admin.id}" name="id"/>
				<input class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
			</div>
		</div>
	</form>
</article>

{include file="pub/footer" /}

<!--请在下方写此页面业务相关的脚本--> 
<script type="text/javascript">

	function getNextArea(o, sl){
		var pid = $(o).val();
		$.getJSON("{:url('getArea')}", {pid:pid}, function(res){
			var data = res.data;
			var _l = "<option value=''>请选择</option>";
			if(sl == "city"){
				$(".country").html(_l);
			}
			$.each(data, function (i,v) {
				_l += '<option value="'+v.id+'">'+v.name+'</option>';
			});
			$("."+sl).html(_l);
		})
	}

	function showAgent(role_id, o){
		var is_show = $(o).prop("checked");
		if(role_id == 15){
			is_show?$(".agent").show():$(".agent").hide();
		}
	}

    $(function(){
        var $dep_list = {$dep_list};
        var opts = '';
        $.each($dep_list, function(i, v){
            if({$admin['department_pid']} == v.id){
                opts += '<option value="'+i+'" selected>'+v.name+'</option>';
            }else{
                opts += '<option value="'+i+'">'+v.name+'</option>';
			}
        });
        $("#department_pid1").html(opts);
        var cateId = $("#department_pid1").val();
        $("#department_pid").val($dep_list[cateId].id);
        var opts1= '';
        $.each($dep_list[cateId].children, function (i1,v1) {
            if(v1.id == {$admin['department_id']}){
                opts1 += '<option value="'+v1.id+'" selected>'+v1.name+'</option>';
            }else{
                opts1 += '<option value="'+v1.id+'">'+v1.name+'</option>';
			}
        });
        $("#department_id").html(opts1);
        $("#department_pid1").on('change', function () {
            var id = $(this).val();
            $("#department_pid").val($dep_list[id].id);
            var opts2 = "";
            $.each($dep_list[id].children, function (i1,v1) {
                opts2 += '<option value="'+v1.id+'">'+v1.name+'</option>';
            });
            $("#department_id").html(opts2);
        });



        $("#form-admin-edit").validate({
            rules:{
                uname:{
                    required:true,
                    minlength:4,
                    maxlength:10,
                    remote:{
                        url: '{:url("Admin/checkName")}',     //后台处理程序
                        type: "post",               //数据发送方式
                        dataType: "json",           //接受数据格式
                        data: {                     //要传递的数据
                            id: function(){
                                return $("#adminId").val();
                            }
                        }
                    }
                },
                "role_id[]":{
                    required:true,
                    minlength:1
                }
            },
            messages:{
                uname:{
                    required: '用户名不能为空',
                    minlength: '最少不能少于4位',
                    maxlength: '最多不能多余10位',
                    remote: '用户名已存在'
                },
                "role_id[]":{
                    required:'角色必须选择',
                    minlength:'至少选择一个角色'
                }
            },
            submitHandler:function(form){
                $(form).ajaxSubmit({
                    type: 'post',
                    url: "{:url('Admin/adminEdit')}" ,
                    success: function(data){
                        if(1 == data.code){
                            var index = parent.layer.getFrameIndex(window.name);
                            layer.msg(data.msg, {icon:6, time:1000});
                            setTimeout(function(){
                                parent.location.reload();
                                parent.layer.close(index);
                            }, 1000);
                        }else{
                            layer.msg(data.msg, {icon:6, time:1000});
                        }
                    },
                    error: function(XmlHttpRequest, textStatus, errorThrown){
                        layer.msg('error!',{icon:1,time:1000});
                    }
                });
            }
        });
    });
</script> 
<!--/请在上方写此页面业务相关的脚本-->
</body>
</html>